home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / COMMS / C035.ZIP / JMODEM_C.DOC < prev    next >
Text File  |  1990-02-17  |  18KB  |  473 lines

  1.  
  2.                                   J M O D E M
  3.                          *  The Microsoft C Version  *
  4.                                February 17, 1990
  5.                                Richard B. Johnson
  6.                               405 Broughton Drive
  7.                           Beverly Massachusetts 01915
  8.                                BBS (508) 922-3166
  9.  
  10.           Introduction.
  11.  
  12.           JMODEM was first introduced about two years ago. It has en-
  13.           joyed a steady increase in popularity around the world. It
  14.           has even been explained in some detail in John Dvorak's book
  15.           on PC communications; Dvorak's Guide to PC Telecommunica-
  16.           tions, 1990, Osborne-McGraw-Hill, 2600 Tenth Street,
  17.           Berkely, CA.
  18.  
  19.           JMODEM was first written in assembly language. Since this
  20.           language is hard to read and use, it has been difficult for
  21.           communications program developers to incorporate it into
  22.           their programs directly. Instead it must be executed as an
  23.           external protocol. Now JMODEM has been written in the C
  24.           Language. C has become the de-facto standard for portable
  25.           code development even though there isn't presently any code
  26.           that is truly portable across many different machines. The
  27.           writing of JMODEM in C will make it easier for software de-
  28.           velopers throughout the world to use this very useful pro-
  29.           tocol.
  30.  
  31.           You can use this new version of JMODEM just as the older
  32.           versions written in assembly. It has a new color sign-on
  33.           screen and status-block windows that overlap. Although the
  34.           new JMODEM.EXE code is larger than the assembly-language
  35.           version, JMODEM.COM, the program still executes very fast
  36.           because much effort has been taken to streamline the C code.
  37.           JMODEM.COM required 64 k of RAM (one segment) to execute
  38.           properly. JMODEM.EXE requires 79 k to allow the screens to
  39.           be written properly, and 66 k of free RAM to execute without
  40.           aborting although the previous screen content will be lost.
  41.           Like all versions of JMODEM, this version is 100% compatible
  42.           with all previous version including Beta version 1.00. From
  43.           it's inception, the essential structure of JMODEM has never
  44.           been changed.
  45.  
  46.           How to install JMODEM.
  47.  
  48.           JMODEM executes best from a batch file as an external pro-
  49.           tocol for any of the communications programs that have ex-
  50.           ternal-protocol capability. A typical communications program
  51.           is TELIX.
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.                                      - 1 -
  60.                                                JMODEM, the C Version
  61.  
  62.  
  63.  
  64.           Here is a batch file used with TELIX for uploads:
  65.  
  66.           @ECHO OFF
  67.           Rem * JMODEM TELIX Upload batch file.
  68.           C:\TELIX\JMODEM S1 %3
  69.           Rem           | ||  |_________ file name (passed by TELIX)
  70.           Rem           | ||____________ COM port (1 - 4)
  71.           Rem           | |_____________ Send
  72.           Rem           |_______________ Path and name of JMODEM
  73.  
  74.  
  75.           This is a batch file used with TELIX for downloads:
  76.  
  77.           @ECHO OFF
  78.           Rem * JMODEM TELIX Download batch file.
  79.           C:\TELIX\JMODEM R1 %3
  80.           Rem           | ||  |_________ file name (passed by TELIX)
  81.           Rem           | ||____________ COM port (1 - 4)
  82.           Rem           | |_____________ Receive
  83.           Rem           |_______________ Path and name of JMODEM
  84.  
  85.  
  86.           This is a batch file used for PCPLUS (PROCOMM) uploads:
  87.  
  88.           @ECHO OFF
  89.           Rem * JMODEM PCPLUS Upload batch file.
  90.           C:\TELIX\JMODEM S1 %1
  91.           Rem           | ||  |_________ file name (passed by PCPLUS)
  92.           Rem           | ||____________ COM port (1 - 4)
  93.           Rem           | |_____________ Send
  94.           Rem           |_______________ Path and name of JMODEM
  95.  
  96.  
  97.           This is a batch file for PCPLUS (PROCOMM) ownloads:
  98.  
  99.           @ECHO OFF
  100.           Rem * JMODEM PCPLUS Download batch file.
  101.           C:\TELIX\JMODEM R1 %1
  102.           Rem           | ||  |_________ file name (passed by PCPLUS)
  103.           Rem           | ||____________ COM port (1 - 4)
  104.           Rem           | |_____________ Receive
  105.           Rem           |_______________ Path and name of JMODEM
  106.  
  107.           You can chop this text out with an editor and create your
  108.           batch files quickly and correctly. PCPLUS passes the file-
  109.           name as the %1 parameter and TELIX uses the %3 parameter.
  110.           JMODEM does not need to know anything else about your system
  111.           to operate. It does not even need to know the baud-rate or
  112.           other communications parameters. All it needs to know about
  113.           is the file name and the communications adapter port to use.
  114.  
  115.  
  116.  
  117.  
  118.                                      - 2 -
  119.                                                JMODEM, the C Version
  120.  
  121.  
  122.  
  123.           JMODEM doesn't care if you are using a 19,200 baud modem
  124.           with the DTE locked at high-speed or a 300 baud modem on a
  125.           lamp-cord. It is very smart about communications and handles
  126.           lost carriers (hangup) and flow-control in a very simple way
  127.           which will be explained in detail later.
  128.  
  129.           If you only want to try JMODEM without having to install it,
  130.           you need only to activate the JMODEM protocol on a BBS sys-
  131.           tem, then "shell to DOS" and execute it manually from the
  132.           DOS prompt. You will be pleasantly surprised at how rapidly
  133.           it can transfer a file and how well it executes over net-
  134.           works.
  135.  
  136.           How JMODEM works.
  137.  
  138.           JMODEM uses variable-length records called blocks. These
  139.           blocks start with 512 data-bytes and increase in length to a
  140.           maximum of 8192 bytes per block. There is a 6-byte overhead
  141.           associated with each block so the percentage of overhead
  142.           starts at a fairly high 0.1 percent and decreases to a very
  143.           low 0.07 percent as the transmission progresses. The block
  144.           length will increase in 512-byte increments as long as there
  145.           are no errors requiring retransmission. Should an error
  146.           occur, the block-size is cut in half. This continues until
  147.           the block-size is as short as 64 bytes.
  148.  
  149.           The blocks may actually be of any length but never exceed
  150.           the maximum allowed. An attempt is made to reduce the amount
  151.           of data that needs to be transmitted by compressing each
  152.           block before transmission. Since much data is already com-
  153.           pressed, being from ".ZIP" files and such, it is possible
  154.           that "compression" may actually cause an increase in block-
  155.           length. JMODEM will send a compressed block only if it is
  156.           shorter than the non-compressed block.
  157.  
  158.           Since it takes time to compress and expand data, only the
  159.           simplest and quickest compression method is used. It is very
  160.           effective with ".EXE" files that contain large blocks of
  161.           binary nulls and most text files but it is not very effec-
  162.           tive with highly compressed archive files. The compression
  163.           method is called "run-length-limited" or more explicitly
  164.           "how many of what kind". Basically, the block is searched
  165.           for groups if similar characters. If many similar characters
  166.           are found, compression consists of sending a sentinel byte
  167.           of hexadecimal BB, followed by a two-byte amount, and this
  168.           followed by the byte to be repeated.
  169.  
  170.           In the following example we have a string of spaces (20
  171.           hex).
  172.  
  173.  
  174.  
  175.  
  176.  
  177.                                      - 3 -
  178.                                                JMODEM, the C Version
  179.  
  180.  
  181.  
  182.           Before compression:
  183.                                ( 18 bytes )
  184.             20 20 20 20 20 20 20 20 20 20 20 34 37 87 EF FF 3A 23
  185.  
  186.  
  187.           After compression:
  188.                                ( 11 bytes )
  189.             BB 0B 00 20 34 37 87 EF FF 3A 23
  190.  
  191.           The blocks could actually get longer because the sentinel
  192.           byte could be present in the data:
  193.  
  194.           Before compression:
  195.                                ( 9 bytes )
  196.             BB 00 BB AF EF BB 00 AE EF
  197.  
  198.  
  199.           After compression:
  200.                                ( 16 bytes )
  201.             BB 01 00 BB 00 BB 01 00 BB AF BB 01 00 BB AE EF
  202.  
  203.           As soon as the encoded length exceeds the data block length,
  204.           compression is abandoned and the non-compressed block is
  205.           sent. JMODEM sends a byte that tells the receiver if the
  206.           data is compressed or not. This same byte tells the receiver
  207.           when the end-of-file has occurred so JMODEM is able to
  208.           preserve exact file-length.
  209.  
  210.                                The JMODEM block.
  211.  
  212.  
  213.           The JMODEM block looks like this:
  214.  
  215.            |< --------- First byte sent / received
  216.           20 00 01 01 .. .. .. .. .. AE 01
  217.            |  |  |  |        |        |  |___ CRC (high byte)
  218.            |  |  |  |        |        |______ CRC (low byte)
  219.            |  |  |  |        |_______________ data bytes
  220.            |  |  |  |________________________ type of block
  221.            |  |  |___________________________ block number
  222.            |  |______________________________ block length (high byte)
  223.            |_________________________________ block length (low byte)
  224.  
  225.  
  226.           The Block length:
  227.  
  228.           The block length is a WORD (16 bits). This allows the blocks
  229.           to be 65,535 bytes long although in practice the length is
  230.           not allowed to exceed 8192 bytes (plus overhead). The block-
  231.           length is the length of the entire JMODEM block, not just
  232.           the data-bytes.
  233.  
  234.  
  235.  
  236.                                      - 4 -
  237.                                                JMODEM, the C Version
  238.  
  239.  
  240.  
  241.           The Block number:
  242.  
  243.           The block number is a BYTE. It starts at 1 and becomes zero
  244.           again after block 255. It is used to make certain that all
  245.           receiption errors are detected.
  246.  
  247.           The Block type:
  248.  
  249.           This BYTE is bit-mapped to tell the receiver what kind of
  250.           block has been sent. Presently there are three kinds of
  251.           blocks:
  252.  
  253.                 00000000B       Hex 00  ( Normal data )
  254.                 00000001B       Hex 01  ( Compressed data )
  255.                 00000010B       Hex 02  ( End of file )
  256.  
  257.           The CRC:
  258.  
  259.           The CRC is a WORD (16 bits). It is not a "standard" type of
  260.           CRC because, unless done with hardware, a standard 16-bit
  261.           polynoimial would take several seconds to calculate for a
  262.           long block. Instead it uses a rapid rotate and sum algoritm
  263.           that is probably just as effective as the more "standard"
  264.           polynominials.
  265.  
  266.           The CRC is generated using this polynomial:
  267.  
  268.           X =  X + X^(2(n-mod 7)).......  Where n = t(n-1)
  269.                                          And t = string length
  270.  
  271.           It has the advantage of simplicity in assembly-language
  272.           programming and will detect errors with a probability of
  273.           about  one undetected error in 2^132 (which is a very large
  274.           number). It does not correct errors so its not important to
  275.           use some "standard" function to generate the CRC.
  276.  
  277.           The C code shows how easy it is to create and check this
  278.           kind of CRC.
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.                                      - 5 -
  296.                                                JMODEM, the C Version
  297.  
  298.  
  299.                            The JMODEM Communications
  300.                                 Hardware Control
  301.  
  302.           When JMODEM is first loaded for execution it checks the
  303.           state of the modem-control leads. If there is no modem
  304.           carrier detected, it assumes that you have connected two PCs
  305.           together without a modem and will not bother to check for a
  306.           dropped carrier during execution.
  307.  
  308.           Since JMODEM must exercise flow-control so it can be used
  309.           with high-speed modems with fixed baud rates, it also as-
  310.           sumes that at the time at which it is first executed, the
  311.           modem will be requesting data because there will have been
  312.           very little I/O over the previous few seconds. JMODEM stores
  313.           the state of RTS/CTS and DTR/DSR and uses this as a refer-
  314.           ence. When transmitting data, should JMODEM find that the
  315.           state of these modem-control leads has changed, it waits
  316.           until the modem-control leads have reverted back to the
  317.           initial state before sending any more data. This allows ANY
  318.           modem to exercise flow-control with JMODEM, even those that
  319.           use "pin 4" instead of "pin 20". When JMODEM is waiting, it
  320.           checks to verify that the modem carrier has not been drop-
  321.           ped. If the carrier is dropped, or if the user aborts,
  322.           JMODEM will exit, setting a DOS ERRORLEVEL code.
  323.  
  324.           You can abort JMODEM at any time by hitting Ctrl-Break or
  325.           Ctrl-C. It will take several seconds for JMODEM to abort
  326.           because it does not continually check these keys. Unlike
  327.           previous versions, JMODEM erases the files from aborted
  328.           downloads. Also, JMODEM will abort if it is unable to rename
  329.           a file in the following example.
  330.  
  331.           Suppose you wish to download a file called VIRUS.EXE. Sup-
  332.           pose also that VIRUS.EXE already exists. Instead of over-
  333.           writing your previous version of VIRUS.EXE, JMODEM has al-
  334.           ways been nice and renamed it to VIRUS.OLD before creating a
  335.           new file. With previous versions of JMODEM, if VIRUS.OLD
  336.           already existed, JMODEM would have deleted it before renam-
  337.           ing the present file to ".OLD". This no longer is done.
  338.           JMODEM never deletes ANYTHING anymore except it's own abort-
  339.           ed download. This should reduce the number of threats I have
  340.           received!
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.                                      - 6 -
  355.                                                JMODEM, the C Version
  356.  
  357.  
  358.                                The C programmer's
  359.                                 Guide to JMODEM
  360.  
  361.           Included within this package should be the following files:
  362.           JMODEM          This is the MAKE file
  363.           JMODEM_A  C     Contains the _main() routine entry point.
  364.           JMODEM_B  C     Allocates memory, parses input strings.
  365.           JMODEM_C  C     All of the file I/O is in this file.
  366.           JMODEM_D  C     Data compression/expansion, CRC calculation
  367.           JMODEM_E  C     Communications I/O interrupt service, etc.
  368.           JMODEM_F  C     Screen I/O (windows, etc)
  369.           JMODEM    H     Contains JMODEM globals and data structures.
  370.           SCREEN    H     Screen definitions, prototypes, structures
  371.           UART      H     Definitions for the 8250 UART
  372.  
  373.           If you have MicroSoft 'C' version 5.0 or later, and if your
  374.           environment and paths are properly set up, you should be
  375.           able to type:
  376.  
  377.                 MAKE JMODEM
  378.                 ... and a brand new version of JMODEM will be created.
  379.  
  380.           Just as in the MASM language files, the source code is
  381.           strongly-typed. All function prototypes are declared and no
  382.           defaults are used. You can compile at warning-level 3, the
  383.           most stringent level available with Microsoft compilers, and
  384.           you will get no errors at all.
  385.  
  386.           You may modify JMODEM for your own use, but  P L E A S E  do
  387.           NOT distribute the modified version on BBS systems because I
  388.           do not wish to support 222,500 versions of JMODEM! If you
  389.           find a bug, or wish to improve or add something that will
  390.           increase the value of JMODEM without making it incompatible
  391.           with previous versions, please upload your improvements to
  392.           my BBS system and I may include the revisions (with your
  393.           name attached) in an upcoming version. Note that there are 6
  394.           "type" bits available in the JMODEM control-byte that could
  395.           be used to tell the receiver that another file is coming,
  396.           etc., (for batch). Things like this could be added without
  397.           destroying compatibility with previous versions.
  398.  
  399.           MicroSoft C seems to have several bugs one of which affects
  400.           files that are very long (over 256k). My first attempts to
  401.           use C files under MicroSoft for JMODEM used the stream-I/O
  402.           (FILE *) type of files. This resulted in corruption of long
  403.           files. Therefore I implemented the UNIX/DOS type of file-I/O
  404.           that uses handles rather than file-control blocks. These
  405.           work rather well.
  406.  
  407.           Another bug is in the SMALL C run-time library. Although you
  408.           can compile the source to use the SMALL library version, and
  409.  
  410.  
  411.  
  412.  
  413.                                      - 7 -
  414.                                                JMODEM, the C Version
  415.  
  416.  
  417.           the compiler / linker execute without errors, the resulting
  418.           program will not work because the memory allocation routines
  419.           dump the allocated buffers on top of each other! This re-
  420.           sults in VERY corrupt files, bearing no resemblance to the
  421.           originals! Stay with the medium models and you will have no
  422.           problems.
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.                                      - 8 -
  473.